# -*-coding:utf-8-*-
# file_name = input('请输入要打开的文件名：')
# f = open(file_name, 'r')
# print('文件内容是：')
# for each_line in f:
#     print(each_line)

# 1.AssertionError:断言语句（assert）失败
# my_list = ['小甲鱼']
# assert len(my_list) > 0
# my_list.pop()
# assert len(my_list)

# 2.AttributeError:尝试访问未知对象的属性
my_list = []
# my_list.fishc

# 3.IndexError:索引超出序列的范围
my_list = [1, 2, 3]
# my_list[3]

# 4.KeyError:字典中查找一个不存在的关键字
my_dict = {"one": 1, "two": 2, "three": 3}

# my_dict["one"]
# my_dict["four"]

# 5.访问一个不存在变量
# fishc

# 6.OSError:操作系统产生的异常

# 7.SyntaxError：Python的语法错误
# print "I love fishc.com"

# 8.TypeError:不同类型间的无效操作
# print (1+"1")
# print (str(1)+"1")
# print (1+int("1"))

# 9.ZeroDivisionError:除数为零
# 5/0

# try_except 语句
# f = open('我为什么是一个文档.txt')
# print (f.read())
# f.close()

# try:
#     f = open('我为什么是一个文档.txt')
#     print (f.read())
#     f.close()
# except OSError as reason:
#     print ("文件出错啦 T_T\n 错误原因是："+str(reason))

# try:
#     sum = 1 + "1"
#     f = open('我是一个不存在的文档.txt')
#     print(f.read())
#     f.close()
# except OSError as reason:
#     print("文件出错啦 T_T\n 错误原因是：" + str(reason))
# except TypeError as reason:
#     print("类型出错啦 T_T\n 错误原因是：" + str(reason))

# try:
#     int('abc')
#     sum = 1 + '1'
#     f = open('我是一个不存在的文档.txt')
#     print(f.read())
#     f.close()
# except (OSError, TypeError):
#     print('出错啦 T_T\n 错误原因是：' + str(reason))

# try:
#     f = open('我是一个不存在的文档.txt')
#     print(f.read())
#     sum = 1 + '1'
#     f.close()
# except:
#     print('出错啦')

# try:
#     f = open('我是一个不存在的文档.txt')
#     print(f.read())
#     sum = 1 + '1'
# except:
#     print('出错啦')
# finally:
#     f.close()

# raise 自己抛出异常
# raise ZeroDivisionError
# raise ZeroDivisionError('除数不能为零！')

# def showMaxFactor(num):
#     count = num // 2
#     while count > 1:
#         if num % count == 0:
#             print('%d 最大的约数是%d' % (num, count))
#             break
#         count -= 1
#     else:
#         print('%d 是素数！' % num)
#
#
# num = int(input('请输入一个数：'))
# showMaxFactor(num)

# try:
#     int('abc')
# except ValueError as reason:
#     print('出错啦：' + str(reason))
# else:
#     print('没有任何异常！')

# try:
#     f = open('data.txt', 'w')
#     for each_line in f:
#         print(each_line)
# except OSError as reason:
#     print('出错啦：' + str(reason))
# finally:
#     f.close()
# with不用担心忘记关闭文件
try:
    with open('data.txt', 'w') as f:
        for each_line in f:
            print(each_line)
except OSError as reason:
    print('出错啦：' + str(reason))
